package com.gzx.spring.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzx.risk.core.apis.context.ApiContext;
import com.gzx.risk.core.apis.result.RiskResult;
import com.gzx.risk.core.pojo.RiskRuleResultInfo;
import com.gzx.spring.db.entity.RiskRuleResult;
import com.gzx.spring.db.mapper.RiskRuleResultMapper;
import com.gzx.spring.db.service.RiskRuleApiService;
import com.gzx.spring.db.service.RiskRuleResultService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class RiskRuleResultServiceImpl extends ServiceImpl<RiskRuleResultMapper, RiskRuleResult>
		implements RiskRuleResultService {
	@Resource
	private RiskRuleApiService riskRuleApiService;

	@Override
	public void log(ApiContext ctx) {
		RiskRuleResult result = new RiskRuleResult();
		result.setOrdercode(ctx.getOrder().getOrdercode());
		result.setName(ctx.getUserInfo().getName());
		result.setIdCard(ctx.getUserInfo().getId());
		result.setIdLastdate(ctx.getUserInfo().getIdLastdate());
		result.setPhone(ctx.getUserInfo().getPhone());
		result.setSex(ctx.getUserInfo().getSex());
		result.setRuleResult(String.valueOf(ctx.getRuleResult()));
        result.setRiskNode(ctx.getUserInfo().getRiskNode());
		if (ctx.getRejectRule() != null) {
			result.setRejectRule(ctx.getRejectRule());
		} else {
			result.setRejectRule("");
		}
		if (ctx.getRejectMsg() != null) {
			result.setRejectMsg(ctx.getRejectMsg());
		} else {
			result.setRejectMsg("");
		}
		if (ctx.getYushouCount() == null) {
			result.setYushouCount(-1);
		} else {
			result.setYushouCount(ctx.getYushouCount());
		}

		this.save(result);
		log.info("result.id:{},results.size:{}", result.getId(), ctx.getResults().size());
		for (RiskResult riskResult : ctx.getResults()) {
			riskRuleApiService.log(result.getId(), riskResult);
		}
	}

	@Override
	public RiskRuleResultInfo findLastRiskRuleResult(String id) {
		QueryWrapper<RiskRuleResult> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id_card", id).orderByDesc("created_time").last("LIMIT 1");
		List<RiskRuleResult> datas = this.list(queryWrapper);
		if (datas == null || datas.size() == 0) {
			return null;
		}
		RiskRuleResult result = datas.get(0);
		RiskRuleResultInfo info = new RiskRuleResultInfo();
		BeanUtils.copyProperties(result, info);
		return info;
	}

	@Override
	public RiskRuleResult getByOrdercode(String ordercode) {
		QueryWrapper<RiskRuleResult> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ordercode", ordercode).last("LIMIT 1");
		List<RiskRuleResult> datas = this.list(queryWrapper);
		if (datas == null || datas.size() == 0) {
			return null;
		}
		RiskRuleResult result = datas.get(0);
		return result;
	}

    @Override
    public List<RiskRuleResult> getListByOrdercode(String ordercode) {
        QueryWrapper<RiskRuleResult> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ordercode", ordercode);
        List<RiskRuleResult> datas = this.list(queryWrapper);
        if (datas == null || datas.isEmpty()) {
            return Collections.emptyList();
        }
        return datas;
    }

    @Override
    public Boolean isRejectedByIdCard(String id) {
        LambdaQueryWrapper<RiskRuleResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RiskRuleResult::getIdCard, id)
                .ge(RiskRuleResult::getCreatedTime, LocalDateTime.now().minusDays(30))
                .and(w -> w.eq(RiskRuleResult::getRuleResult, false)
                        .or()
                        .eq(RiskRuleResult::getReviewResult, false));
        return this.exists(wrapper);

    }

}
